package com.jiayang.hhotel;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.jiayang.hhotel.ui.BuildingNoActivity;
import com.jiayang.hhotel.ui.BuildingStyleActivity;
import com.jiayang.hhotel.ui.BuildingStyleHisActivity;
import com.jiayang.hhotel.ui.CutPictureActivity;
import com.jiayang.hhotel.ui.SelectPictureActivity;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.DataKeeper;
import com.jiayang.hhotel.util.ImageLoaderUtil;
import com.jiayang.hhotel.util.StringUtil;


import java.io.File;

import butterknife.OnClick;
import cn.droidlover.xdroidmvp.imageloader.ILFactory;
import cn.droidlover.xdroidmvp.mvp.XActivity;

public class MainActivity extends XActivity implements View.OnClickListener{


    private static final String TAG = "MainActivity";
    private static final int REQUEST_TO_SELECT_PICTURE = 20;
    private static final int REQUEST_TO_CUT_PICTURE = 21;
    private Button button;
    private  ImageView imageView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ILFactory.getLoader().init(context);
        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(this);
        imageView=(ImageView)findViewById(R.id.imageview);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public Object newP() {
        return null;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.button:

               //CommonUtil.toActivity(MainActivity.this,SelectPictureActivity.createIntent(context), REQUEST_TO_SELECT_PICTURE, false);
                //CommonUtil.toActivity(MainActivity.this,SelectPictureActivity.createIntent(context), REQUEST_TO_SELECT_PICTURE, false);
                Intent intent=new Intent(context, BuildingStyleActivity.class);
                startActivity(intent);
                break;
        }
    }
    private String picturePath;
    /**裁剪图片
     * @param path
     */
    private void cutPicture(String path) {
        if (StringUtil.isFilePath(path) == false) {
            Log.e(TAG, "cutPicture  StringUtil.isFilePath(path) == false >> showShortToast(找不到图片);return;");
            getvDelegate().toastShort("找不到图片");
            return;
        }
        this.picturePath = path;

        CommonUtil.toActivity(context,CutPictureActivity.createIntent(context, path
                , DataKeeper.imagePath, "photo" + System.currentTimeMillis(), 200)
                , REQUEST_TO_CUT_PICTURE);
    }
    /**显示图片
     * @param path
     */
    private void setPicture(String path) {
        if (StringUtil.isFilePath(path) == false) {
            Log.e(TAG, "setPicture  StringUtil.isFilePath(path) == false >> showShortToast(找不到图片);return;");
            getvDelegate().toastShort("找不到图片");
            return;
        }
        this.picturePath = path;
        getvDelegate().toastShort(path);
        //svDemoMain.smoothScrollTo(0, 0);

        //ILFactory.getLoader().loadFile(imageView, new File(path), null);
       ImageLoaderUtil.loadImage(imageView, path);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_SELECT_PICTURE:
                if (data != null) {
                    cutPicture(data.getStringExtra(SelectPictureActivity.RESULT_PICTURE_PATH));
                }
                break;
            case REQUEST_TO_CUT_PICTURE:
                if (data != null) {
                    setPicture(data.getStringExtra(CutPictureActivity.RESULT_PICTURE_PATH));
                }
                break;
        }
    }
}
